home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / lib / obsolete / friedman.pro < prev    next >
Text File  |  1997-07-08  |  4KB  |  160 lines

  1. ; $Id: friedman.pro,v 1.2 1997/01/15 04:02:19 ali Exp $
  2. ;
  3. ;  Copyright (c) 1991-1997, Research Systems Inc.  All rights
  4. ;  reserved. Unauthorized reproduction prohibited.
  5.  
  6.  
  7. function compute_rank, x , SortD
  8. list = where( sortD EQ x,count)
  9. return, list(0) + (count-1)/2.0
  10. END
  11.          
  12.   
  13. pro Friedman, Data, Rank, F, Prob, DF, Names=names, List_Name=Ln,$
  14.               NoPrint=NP
  15. ;+ 
  16. ; NAME:
  17. ;      FRIEDMAN
  18. ;
  19. ; PURPOSE:
  20. ;    Perform a two-way analysis of variance with k treatments and b 
  21. ;    blocks to test the hypothesis that all treatments have the same
  22. ;    distribution versus the alternative that at least two treatment 
  23. ;    distributions differ in location. No assumptions are needed about 
  24. ;    the underlying probability distributions.
  25. ;        
  26. ; CATEGORY:
  27. ;    Statistics.
  28. ;
  29. ; CALLING SEQUENCE:
  30. ;    FRIEDMAN, Data [, Rank, F, Prob, Df]
  31. ;
  32. ; INPUTS:
  33. ;    Data:    two dimensional array. Data(i,j) = the observation from the
  34. ;        ith treatment and jth block.
  35. ;
  36. ; KEYWORDS:
  37. ;    NAMES:    vector of names for the populations to be used in the output.
  38. ;
  39. ;    LIST_NAME:    name of output file. Default is to the screen.
  40. ;
  41. ;      NOPRINT:    a flag, if set, to suppress output to the screen or a file.    
  42. ;
  43. ; OUTPUT:
  44. ;    Table written to the screen showing rank sum for each treatment.
  45. ;    Also, the Friedman test statistic and it is probability assuming a 
  46. ;    chi-square distribution are written to the screen.
  47. ;
  48. ; OPTIONAL OUTPUT PARAMETERS:
  49. ;    Rank:    1-dim array of rank sums.  Rank(i) = sum of ranks of 
  50. ;        population i.
  51. ;
  52. ;    F:    Friedman test statistic.
  53. ;
  54. ;    Prob:    probability of F, assuming a chi-square distribution.
  55. ;
  56. ;    DF:    degrees of freedom of chi-square distribution.
  57. ;        
  58. ; RESTRICTIONS:
  59. ;    No missing data
  60. ;
  61. ; COMMON BLOCKS:
  62. ;    None.
  63. ;
  64. ; PROCEDURE:   
  65. ;    For each block, the observations for the k treatments are ranked. 
  66. ;    Let Ri = rank sum for ith treatment, RRi = Ri/b and Let R = average 
  67. ;    of all ranks =(k+1)/2. Let RRi = Ri/ni.  The rank sum analogue to the
  68. ;    standard sum of squares is:
  69. ;        SS =b* sum((RRi -R)^2).
  70. ;    The Friedman statistic F = 12/(k(k+1)) * V and has approximately the
  71. ;    chi-square distribution if each sample size exceeds 4.
  72. ;-
  73.  
  74.  
  75.  
  76.  
  77. On_Error,2
  78. SD= size(Data)
  79.  
  80. if( N_Elements( Ln) NE 0) THEN openw,unit,/get,Ln else unit=-1
  81.  
  82.  
  83. if ( SD(0) NE 2) THEN BEGIN
  84.    printf,unit, 'friedman- Data array has wrong dimension'
  85.    goto, DONE
  86. ENDIF
  87.  
  88. C=SD(1)
  89. R= SD(2)
  90.  
  91. Rank = Fltarr(C) ;
  92.  
  93. m = 2*max(abs(Data)) + 1
  94. if ( m LT 1.0e30/R) THEN BEGIN         ; fast sort
  95.   Temp = Replicate(m,C) # (findgen(R))
  96.   SortD = Data + Temp   
  97.   SortD = SortD(sort(sortD))              ; sort data by rows
  98. ENDIF ELSE BEGIN
  99.   m=0              ;we wont use m since it may cause overflow
  100.  
  101. SortD = Data
  102.   for i = 0L,R-1 DO SortD(i*C:(i+1)*C-1)=            $
  103.                             Data(sort(Data(*,i)),i) 
  104. ENDELSE
  105.  
  106.  
  107. for i = 0L,C-1 DO BEGIN               ; Compute rank sums
  108.  
  109.    Rank(i) = 0
  110.    
  111.    for j = 0L,R-1  DO BEGIN
  112.            x = data(i,j) + j*m
  113.            x = compute_rank(x,SortD(j*C:(j+1)*C-1))
  114.            Rank(i) = Rank(i) + x +1
  115. next:
  116.         ENDFOR
  117. ENDFOR
  118.  
  119.  
  120. F =12.0/(float(R)*C*(C+1.)) * Total(Rank^2) - 3*float(R)*(C+1.)
  121.  
  122.  
  123.  
  124.  
  125.  SN =Size(Names)
  126.  if (SN(1) EQ 0) THEN BEGIN
  127.      I = INDGEN(C)
  128.      Names=['treatment'+StrTrim(I,2)]  
  129.  ENDIF ELSE                  $
  130.    if ( SN(1) LT C) THEN BEGIN
  131.      I = Indgen(C)
  132.      printf,unit,'sign_test- missing names'
  133.      Names=[Names, 'treatment'+StrTrim(I(SN(1):C-1),2)]
  134.   ENDIF
  135.  
  136.  
  137.   DF = C-1
  138.   Prob = 1 - chi_sqr1(F,DF)
  139.   if(NOT KEYWORD_SET(NP)) THEN BEGIN  
  140.   printf,unit, " Table of Rank Sums:"
  141.   printf,unit, " Friedman Test"
  142.   printf,unit, " "
  143.   printf,unit, "    Treatment        count     Rank Sum"
  144.   for i= 0L,C-1 do                       $
  145.      printf,unit,              $
  146.         format='(A13,5x,I8,5X,G15.7)',Names(i),R,Rank(i)
  147.   printf,unit, " "
  148.   printf,unit,          $
  149.      format = '( " The Friedman F  statistic = ",G15.5)',F
  150.   printf,unit,           $
  151.  format='(" probability =",G15.7," degrees of freedom =",I15)',$
  152.           Prob,DF
  153. ENDIF
  154.  
  155. DONE:
  156.    if ( unit NE -1) THEN Free_Lun,unit
  157.  
  158.     RETURN
  159.     END
  160.